# coding:utf-8

"""
tf_demo01_intro
Created on 2016/12/8 22:09
@author: GuoYufu
@group : OceanHorn
@contact: OceanHorn@163.com
"""

import tensorflow as tf
import numpy as np

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# create tensorflow structure start

weights = tf.Variable(tf.random_uniform(shape=[1], minval=-1.0, maxval=1.0))
biases = tf.Variable(tf.zeros(shape=[1]))

y = weights * x_data + biases

loss = tf.reduce_mean(
    input_tensor=tf.square( y - y_data )
    )
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5)  # 优化器

train = optimizer.minimize(loss=loss) # 训练器

initialize = tf.global_variables_initializer()  # 用于在run时初始化所有变量，

# create tensorflow structure end

session = tf.Session() # 定义session ， 运行时
session.run(fetches=initialize) # 激活整个神经网络

for step in range(1201):
    session.run(fetches=train)
    if step % 20 == 0:
        print step, session.run(fetches=weights), session.run(fetches=biases)

